﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;

namespace TST.SharePointObjects
{
    internal abstract class SiteObjectList
    {
        private SPSite _site;
        private SPWeb _web;
        private Guid _farmID;
        private List<SiteObject> _objects;
        private LicensingMode _licensingMode;
        protected SharePointObjectsDatabase _database;
        private List<SiteObject> _indexedObjects;
        private List<SiteObject> _nonIndexedObjects;

        protected LicensingMode LicensingMode
        {
            get { return _licensingMode; }
        }
        
        protected SPSite Site
        {
            get { return _site; }
        }

        protected SPWeb Web
        {
            get { return _web; }
        }

        internal SiteObjectList(LicensingMode licensingMode, Guid farmID, SharePointObjectsDatabase database)
        {
            _objects = new List<SiteObject>();
            _licensingMode = licensingMode;
            _farmID = farmID;
            _database = database;
        }

        internal SiteObjectList(LicensingMode licensingMode, SPSite site, SPWeb web, Guid farmID, SharePointObjectsDatabase database)
        {
            _objects = new List<SiteObject>();
            _licensingMode = licensingMode;
            _site = site;
            _web = web;
            _farmID = farmID;
            _database = database;
        }

        protected Guid FarmID
        {
            get { return _farmID; }
        }

        protected abstract void doLoadFromWeb();

        protected abstract void doLoadFromWeb(string groupFilter, string nameSearch);

        public void LoadFromWeb()
        {
            if (_web == null)
                return;
            Clear();
            doLoadFromWeb();
            if (Items.Count > 0)
            {
                GetDatabaseProperties();
            }
        }

        public void LoadFromWeb(string groupFilter, string nameSearch)
        {
            if (_web == null)
                return;
            Clear();
            doLoadFromWeb(groupFilter, nameSearch);
            if (Items.Count > 0)
            {
                GetDatabaseProperties();
            }
        }

        public void Clear()
        {
            _objects.Clear();
        }

        public void Add(SiteObject SiteObject)
        {
            _objects.Add(SiteObject);
        }

        public List<SiteObject> Items
        {
            get
            {
                return _objects;
            }
        }

        public List<SiteObject> IndexedItems
        {
            get
            {
                _indexedObjects = _objects.FindAll(
                            delegate(SiteObject so)
                            {
                                return so.Index;
                            }
                        );
                return _indexedObjects;
            }
        }

        public List<SiteObject> ItemsNotIndexed
        {
            get
            {
                _nonIndexedObjects = _objects.FindAll(
                            delegate(SiteObject so)
                            {
                                return !so.Index;
                            }
                        );
                return _nonIndexedObjects;
            }
        }        

        public SiteObject GetItemById(string id)
        {
            foreach (SiteObject obj in _objects)
            {
                if (string.Compare(obj.Id, id, true) == 0)
                {
                    return obj;
                }
            }
            return null;
        }

        public abstract SiteObjectType SiteObjectType
        {
            get;
        }

        public void Sort(string expression, SortDirection direction)
        {
            switch (expression.ToLower())
            {
                case "name":
                    switch (direction)
                    {
                        case SortDirection.Ascending:
                            if (_indexedObjects != null && _nonIndexedObjects != null)
                            {
                                _indexedObjects.Sort(SiteObjectComparer.CompareByName);
                                _nonIndexedObjects.Sort(SiteObjectComparer.CompareByName);
                            }
                            else
                            {
                                _objects.Sort(SiteObjectComparer.CompareByName);
                            }
                            break;
                        case SortDirection.Descending:
                            if (_indexedObjects != null && _nonIndexedObjects != null)
                            {
                                _indexedObjects.Sort(SiteObjectComparer.CompareByNameDesc);
                                _nonIndexedObjects.Sort(SiteObjectComparer.CompareByNameDesc);
                            }
                            else
                            {
                                _objects.Sort(SiteObjectComparer.CompareByNameDesc);
                            }
                            break;
                    }
                    break;
            }
        }

        private void GetDatabaseProperties()
        {
            SiteObjectStore store = new SiteObjectStore(_farmID, Site.WebApplication.Id, Site.ID, Web.ID, _database);
            DataTable table = store.GetSiteObjects(SiteObjectType);
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    string id = row["ObjectId"].ToString();
                    foreach (SiteObject siteObject in Items)
                    {
                        if (string.Compare(siteObject.Id, id, true) == 0)
                        {
                            store.ReadDataRow(row, siteObject);
                            break;
                        }
                    }
                }
            }
        }

    }
}
